﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using WEB.BLL.Services;
using WEB.BLL.Entities;
using WEB.Core.Util;
using Ext.Net;

namespace WEB.Core.Controls
{
    public partial class BasePage
    {
        #region
        private Util.Permission _Permission = new Util.Permission();
        #endregion

        #region
        public Util.Permission Permission
        {
            get { return _Permission; }
            set { _Permission = value; }
        }
        public bool IsView
        {
            get { return _Permission.IsView; }
            set { _Permission.IsView = value; }
        }
        public bool IsAdd
        {
            get { return _Permission.IsAdd; }
            set { _Permission.IsAdd = value; }
        }
        public bool IsEdit
        {
            get { return _Permission.IsEdit; }
            set { _Permission.IsEdit = value; }
        }
        public bool IsDelete
        {
            get { return _Permission.IsDelete; }
            set { _Permission.IsDelete = value; }
        }
        public bool IsPrint
        {
            get { return _Permission.IsPrint; }
            set { _Permission.IsPrint = value; }
        }
        public bool IsImport
        {
            get { return _Permission.IsImport; }
            set { _Permission.IsImport = value; }
        }
        public bool IsExport
        {
            get { return _Permission.IsExport; }
            set { _Permission.IsExport = value; }
        }
        #endregion

        #region
        private void CheckPermission()
        {
            if (User.Identity == null || !User.Identity.IsAuthenticated)
            {
                FormsAuthentication.SignOut();
                FormsAuthentication.RedirectToLoginPage();
                return;
            }
            if (ScreenId.Equals(SCREENS.MAIN) || ScreenId.Equals(SCREENS.CV) || ScreenId.Equals(SCREENS.ABOUT))
            {
                return;
            }

            int RoleId;
            if (CurrentUser != null)
                RoleId = CurrentUser.RoleId;
            else
                RoleId = -3;

            if (RoleId == -1)
            {
                Permission = new Permission(false) { IsEdit = true };
                return;
            }

            if (RoleId == 1)
            {
                Permission = new Permission(true);
                return;
            }

            if (RoleId <= 0)
            {
                FormsAuthentication.RedirectToLoginPage();
                return;
            }

            PermissionService PermissionS = PermissionService.GetInstance();
            PermissionEntity PermissionE = new PermissionEntity();
            PermissionE.RoleId = RoleId;
            PermissionE.ScreenId = ScreenId;
            PermissionE = PermissionS.LoadByPrimaryKey(PermissionE);
            if (PermissionE == null || !PermissionE.IsView)
            {
                FormsAuthentication.RedirectToLoginPage();
                return;
            }
            Permission = new Permission(PermissionE);
            return;
        }

        private void CheckPermissionControl(ref List<Component> ListControl, bool Permission, params string[] ControlID)
        {
            foreach (var item in ControlID)
	        {
                try
                {
                    var _Control = (Component)FindControl(item);
                    if (null != _Control)
                        AddListControl(ref ListControl, _Control);
                }
                catch
                {
                    continue;
                }
	        }
            ListControl.All(control => control.Hidden = !Permission);
            //foreach (var item in ListControl)
            //    item.Hidden = !Permission;
        }

        public virtual void CheckPermissionControl()
        {
            if (!_CheckPermissionControl)
                return;

            CheckPermissionControl(ref _ListControlAdd, IsAdd, "btnAdd");
            CheckPermissionControl(ref _ListControlEdit, IsEdit, "btnEdit");
            CheckPermissionControl(ref _ListControlDelete, IsDelete, "btnDelete", "mnDelete");
            CheckPermissionControl(ref _ListControlPrint, IsPrint, "btnPrint");
            CheckPermissionControl(ref _ListControlImport, IsImport, "btnImport");
            CheckPermissionControl(ref _ListControlExport, IsExport, "btnExport");

            var _WindowEditor = (BaseControl)FindControl("WindowEditor");
            if (_WindowEditor != null)
                _WindowEditor.Permission = Permission;

            var mnEdit = (MenuItem)FindControl("mnEdit");
            if (mnEdit != null)
                mnEdit.Text = IsEdit ? "Edit" : "View";
        }

        public virtual void CheckPermissionHidden()
        {
            if (!_CheckPermissionHidden)
                return;

            var _hfIsEdit = (Hidden)FindControl("hfIsEdit");
            if (_hfIsEdit != null)
                _hfIsEdit.Value = IsEdit;

            var hfIsDelete = (Hidden)FindControl("hfIsDelete");
            if (hfIsDelete != null)
                hfIsDelete.Value = IsDelete;
        }
        #endregion
    }
}
